
cd "$OUT"


***Data prep: the same across all applications

assert z!=. & d1!=. & d2!=. & y!=.

gen dand=d1*d2
gen dor=d1+d2-(d1*d2)
gen d1pd2=d1+d2

gen d1_mindand=d1-dand
gen d1_mindor=d1-dor

gen dand_y=dand*y
gen mindor_y=(1-dor)*y

gen mindor_plusdand_y=(1-dor+dand)*y
sum y
gen y_br_lb=mindor_plusdand_y+(`r(min)'*d1_mindand)-(`r(max)'*d1_mindor)
gen y_br_ub=mindor_plusdand_y+(`r(max)'*d1_mindand)-(`r(min)'*d1_mindor)

label var z 				" $ Z $ "
label var y					" $ Y $ " 
label var d1				" $ D_{1} $ "
label var d2				" $ D_{2} $ "
label var dand				" $ D_{\land} $ "
label var dor				" $ D_{\lor} $ "
label var d1pd2				" $ D_{1}+D_{2} $ "
label var d1_mindand		" $ D_{1}-D_{\land} $ "
label var d1_mindor			" $ D_{1}-D_{\lor} $ "
label var dand_y			" $ D_{\land}Y $ "
label var mindor_y			" $ (1-D_{\lor})Y $ "
label var mindor_plusdand_y	" $ (1-D_{\lor}+D_{\land})Y $ "
label var y_br_lb			" $ (1-D_{\lor}+D_{\land})Y+Y_{min}(D_{1}-D_{\land})-Y_{max}(D_{1}-D_{\lor}) $ "
label var y_br_ub			" $ (1-D_{\lor}+D_{\land})Y+Y_{max}(D_{1}-D_{\land})-Y_{min}(D_{1}-D_{\lor}) $ "


***Assumptions

foreach yvar in d1_mindand d1_mindor d2 {

eststo: reg `yvar' z $controls [pw= $weight ], vce(cluster $clustervar )				
}

esttab using ${EXP}_assumptions.txt, keep(z) b(3) se(3) nostar nonotes label title("Assumptions $EXP")
eststo clear


***Assumptions per x

local appnd 

foreach yvar in d1_mindand d1_mindor {

local group=1

foreach x1 of global controlsx1 {
foreach x2 of global controlsx2 {
foreach x3 of global controlsx3 {
	
count if `x1'==1 & `x2'==1 & `x3'==1

if r(N)>=2 {

sum z if `x1'==1 & `x2'==1 & `x3'==1

if r(min)==0 {
local nocons 
}
if r(min)==1 {
local nocons noconstant
}

reg `yvar' z [pw= $weight ] if `x1'==1 & `x2'==1 & `x3'==1 , vce( cluster $clustervarx ) `nocons'		

regsave z using ${EXP}_assumptionsperx.dta, p ci level(95) addlabel(group, `group', groupstr, "`x1'&`x2'&`x3'=1", yvar, "`yvar'", nocons, "`nocons'") `appnd'

sleep 500

local appnd append
local group=`group'+1
}
	
}
}
}

}


***Instrumental variables

foreach dvar in d1pd2 d1 d2 dand dor {

eststo: reg `dvar' z $controls [pw= $weight ], vce(cluster $clustervar )				
}

esttab using ${EXP}_IVs.txt, keep(z) b(3) se(3) nostar nonotes label title("First stage $EXP")
eststo clear

foreach dvar in d1pd2 d1 d2 dand dor {

eststo: ivregress 2sls y $controls (`dvar'=z) [pw= $weight ], vce(cluster $clustervar )	
}

esttab using ${EXP}_IVs.txt, keep(d1pd2 d1 d2 dand dor) b(3) se(3) nostar nonotes label title("Second stage $EXP") append
eststo clear


***Bounds

eststo brlb: ivregress 2sls y_br_lb 		$controls (d1 = z) 		[pw = $weight ], vce(cluster $clustervar )  // br lb
regsave d1 using ${EXP}_bounds.dta, p ci level(95) addlabel(assumption, "BR", bound, "LB") 

eststo brub: ivregress 2sls y_br_ub 		$controls (d1 = z) 		[pw = $weight ], vce(cluster $clustervar )  // br ub
regsave d1 using ${EXP}_bounds.dta, p ci level(95) addlabel(assumption, "BR", bound, "UB") append

eststo mtrlb: ivregress 2sls y 				$controls (d1 = z) 		[pw = $weight ], vce(cluster $clustervar )  // mtr lb
regsave d1 using ${EXP}_bounds.dta, p ci level(95) addlabel(assumption, "MTRMTS", bound, "LB") append

			  ivregress 2sls dand_y 		$controls (dand = z) 	[pw = $weight ], vce(cluster $clustervar )  // mts ub -- unstacked regression
local b1=_b[dand]
			  ivregress 2sls mindor_y 		$controls (d1 = z) 		[pw = $weight ], vce(cluster $clustervar )  // mts ub -- unstacked regression
local b2=_b[d1]

preserve

expand 2, gen(samp2) // stacking

gen y_s12 		= dand_y*(1-samp2)+mindor_y*samp2
replace dand 	= dand*(1-samp2)
replace d1		= d1*samp2
gen z_s1 		= z*(1-samp2)
gen z_s2 		= z*samp2

foreach var in $controls {
gen `var'_s1	=`var'*(1-samp2)
gen `var'_s2	=`var'*samp2	
}

eststo mtsub1: ivregress 2sls y_s12 samp2 $controls_s12 (dand d1 = z_s1 z_s2) [pw = $weight ], vce(cluster $clustervar )   // mts ub -- stacked regression

assert round(`b1',7)==round(_b[dand],7) & round(`b2',7)==round(_b[d1],7)

eststo mtsub2: nlcom (dand_d1 : _b[dand]+_b[d1]), post

regsave dand_d1 using ${EXP}_bounds.dta, p ci level(95) addlabel(assumption, "MTRMTS", bound, "UB") append

esttab brlb brub mtrlb mtsub1 mtsub2 using ${EXP}_bounds.txt, keep(d1 dand dand_d1) b(3) se(3) nostar nonotes label title("Bounds $EXP")

eststo clear
	
restore

clear


***Figures

if "$EXP"=="OHIE" | "$EXP"=="LinkedIn" | "$EXP"=="Elite" {


***Figure bounds
	
use ${EXP}_bounds.dta, clear

cd "$INDO"
do ImbensManskiCI.do
cd "$OUT"

if "$EXP"=="OHIE" 		local title "Panel A: Health insurance"
if "$EXP"=="LinkedIn" 	local title "Panel B: LinkedIn training"
if "$EXP"=="Elite" 		local title "Elite university"

if "$EXP"=="OHIE" 		local ytitle "LAFTE"
if "$EXP"=="LinkedIn" 	local ytitle " "
if "$EXP"=="Elite" 		local ytitle "LAFTE"

twoway	(rcap ci_im_lb ci_im_ub assn, color(gs0)) ///
		(rbar coef_lb coef_ub assn, barwidth(.3) color(gs16) lcolor(gs0) lwidth(medium)), ///
		title(" `title'" " ", color(gs0) size(medium)) ytitle("`ytitle'", height(5)) xtitle("") ///
		xscale(range(0.5 2.5)) xlabel(1 "Bounded response" 2 "MTR and MTS", nogrid) /// 
		ylab(#6, nogrid) yline(0, lcolor(gs0) lpattern(solid)) legend(off) ///
		plotregion(lcolor(black)) ///
		saving(${EXP}_figurebounds.gph)


***Figure assumptions		

use ${EXP}_assumptionsperx.dta, clear

replace pval=1 if pval==.

local Ntests=_N
gen alphabonf=0.05/`Ntests'

cd "$INDO"
do Figureassumptionsxlabels.do
cd "$OUT"

sum group
local max=r(max)

preserve
keep if yvar=="d1_mindand" 
		
twoway	(scatter coef group 			if pval>=alphabonf , color(gs0) msymbol(O) msize(medium)) ///
		(rcap ci_upper ci_lower group 	if pval>=alphabonf , color(gs0)) ///
		(scatter coef group 			if pval<alphabonf  , color(gs8) msymbol(O) msize(medium)) ///
		(rcap ci_upper ci_lower group	if pval<alphabonf  , color(gs8)), ///
		title("D{subscript:1}-D{subscript:{&Lambda}}", color(gs0) size(medium)) ytitle("Estimate", height(5)) xtitle("") ///
		xlabel(1(1)`max', valuelabels angle(65) labsize(vsmall) nogrid) /// 
		yscale(range(0)) ylab(#6, nogrid) yline(0, lcolor(gs0) lpattern(solid)) legend(off) ///
		plotregion(lcolor(black)) ///
		saving(${EXP}_figureassumptions_d1mindand.gph)
restore

preserve
keep if yvar=="d1_mindor" 
		
twoway	(scatter coef group 			if pval>=alphabonf , color(gs0) msymbol(O) msize(medium)) ///
		(rcap ci_upper ci_lower group 	if pval>=alphabonf , color(gs0)) ///
		(scatter coef group 			if pval<alphabonf  , color(gs8) msymbol(O) msize(medium)) ///
		(rcap ci_upper ci_lower group	if pval<alphabonf  , color(gs8)), ///
		title("D{subscript:1}-D{subscript:V}", color(gs0) size(medium)) ytitle("Estimate", height(5)) xtitle(" ") ///
		xlabel(1(1)`max', valuelabels angle(65) labsize(vsmall) nogrid) /// 
		yscale(range(0)) ylab(#6, nogrid) yline(0, lcolor(gs0) lpattern(solid)) legend(off) ///
		plotregion(lcolor(black)) ///
		saving(${EXP}_figureassumptions_d1mindor.gph)	
restore		

}		

